<?php namespace Admin\Controller;
use Hdphp\Controller\Controller;
/**
 * 商品控制器
 */
class GoodsController extends CommonController {
	private $model;
	public function __init() {
		parent::__init();
		$this->model=new \Common\Model\Goods;
	}
	public function index()
	{
		$data=$this->model->get();
 		View::with('data',$data);
		View::make();
	}
	//添加
	public function add()
	{
		if(IS_AJAX){
			$gid=$this->model->store();
			if(!$gid) {
				echo json_encode(array('status'=>false,'message'=>$this->model->getError()));exit;
			}
			//循环商品属性
			foreach (Q('post.attr',array()) as $taid => $value) {
				if($value){
					$data = array(
						'gavalue' => $value,
						'type_att_taid' => $taid,
						'goods_gid' =>$gid,
					);
					$model = new \Common\Model\GoodsAtt;
					$model->add($data);
				}
			}
			//循环商品规格插入goods_attr
			foreach (Q('post.spec',array()) as $key => $value) {
				if($value){
					foreach ($value['value'] as $k => $v) {
						$data = array(
						'gavalue' =>$value['value'][$k],
						'type_att_taid' => $key,
						'gprice'=>$value['price'][$k],
						'goods_gid' => $gid,
					);
					$model = new \Common\Model\GoodsAtt;
					$model->add($data);
					}
				}
			}
			echo json_encode(array('status'=>true,'message'=>'添加成功'));
			exit;
		}
		//获得所属分类
		$cate=Data::tree(Db::table('category')->get(),'cname');
		View::with('cate',$cate);
		$brand=Db::table('brand')->get();
		View::with('brand',$brand);
		View::make();
	}
    //删除
    public function del()
    {
    	$gid=Q('get.gid',0,'intval');
		$this->model->del($gid);
		View::success('删除成功',U('index'));
    }
	//修改
	public function edit()
	{
		$gid=Q('get.gid',0,'intval');
		if(IS_POST){
			if(!$this->model->edit($gid)) View::error($this->model->getError());
		   	View::success('编辑成功');
		}
		//获得所属分类
		$cate=Data::tree(Db::table('category')->get(),'cname');
		View::with('cate',$cate);
		$brand=Db::table('brand')->get();
		View::with('brand',$brand);
		$goodsatt=Db::table('goods_att')->join('type_att','type_att_taid','=','taid')->where("goods_gid={$gid}")->get();
		foreach ((array)$goodsatt as $k => $v) {
				//把tavalue字符串变为数组
				$goodsatt[$k]['tvalue']=explode('|', $v['tvalue']);
			}
		View::with('goodsatt',$goodsatt);
		$oldData = $this->model->join('goods_cont','gid','=','goods_gid')->where("gid={$gid}")->find();
		$photo=explode('|',$oldData['photo']); 
		View::with('photo',$photo);
		View::with('oldData',$oldData);
		View::make();
	}
	public function ajaxGetAttr()
	{
		if(IS_AJAX){
			$tid=Q('post.tid',0,'intval');
			$data=Db::table('type_att')->where("type_tid={$tid}")->get();
			//如果有类型属性
			foreach ((array)$data as $k => $v) {
				//把tavalue字符串变为数组
				$data[$k]['tvalue']=explode('|', $v['tvalue']);
			}
			
			echo json_encode($data);
			exit;
		}
	}
	public function upload()
	{
    $file=Upload::path('Upload/l' . date('ymd'))->make();
	    if (empty($file)) {
	        $this->ajax(Upload::getError());
	    } else {
	        $data = $file[0];
	        echo json_encode($data);exit;
	    }
	}
	
}
	
?>